<?php
/**
 * Introduction: 收货地址逻辑
 * @author: 王龙
 * @date: 2016/4/18 16:45
 * @email: wanglong@cdlinglu.com
 */

namespace Common\Logic;

use Common\Model;

class HarvestAddressLogic
{

    static public $model;

    public function __construct()
    {

        self::$model = new Model\HarvestAddressModel();

    }

    /**
     * Introduction: 获取数据
     * @author: 王龙
     * @date: 2016/4/18 16:48
     * @email: wanglong@cdlinglu.com
     * @param $where array 查询条件
     */
    static public function getData($where = array(), $order = 'is_default asc,create_time desc')
    {

        if (!$where['uid'])
            return ['code' => 500, 'msg' => '请登录!'];


        $data = self::$model
            ->where($where)
            ->order($order)
            ->select();

        if (!$data)
            return ['code' => 300, 'msg' => '你还没添加收获地址'];

        return ['code' => 200, 'data' => $data];

    }

    /**
     * Introduction: 获取数据
     * @author: 杨陈鹏
     * @date: 2016/4/18 16:48
     * @email: wanglong@cdlinglu.com
     * @param: $where array 如果ID不存在 则获取当前用户的默认地址
     */
    static public function getOneData($where, $fields = '*')
    {

        if (!intval($where['id']))
            $where['is_default'] = 1;

        $data = self::$model
            ->where($where)
            ->field($fields)
            ->find();

        if (!$data)
            return ['code' => 300, 'msg' => '暂无数据!'];

        return ['code' => 200, 'data' => $data];


    }

    /**
     * Introduction: 创建数据
     * @author: 王龙
     * @date: 2016/4/18 16:48
     * @email: wanglong@cdlinglu.com
     * @param: $data array 插入的数据
     */
    static public function create($data)
    {


        if (!$data['recipient'])
            return ['code' => 300, 'msg' => '收货人姓名不能为空'];

        if (!$data['mobile'])
            return ['mobile' => 300, 'msg' => '收货人电话不能为空'];

        if (!$data['sex'])
            return ['code' => 300, 'msg' => '请选择收货人的性别'];

        if (!$data['province'])
            return ['code' => 300, 'msg' => '请选择省份'];

        if (!$data['city'])
            return ['code' => 300, 'msg' => '请选择市'];

        if (!$data['county'])
            return ['code' => 300, 'msg' => '请选择县区'];

        if (!$data['address'])
            return ['code' => 300, 'msg' => '请输入详细地址'];


        $data['create_time'] = NOW_TIME;

        if ($data['is_default'] && $data['is_default'] == 1)
            self::$model->where(['uid' => $data['uid']])->save(['is_default' => 2]);


        //如果是第一条数据,自动设置成默认
        $total = self::$model->where(['uid' => $data['uid']])->count();

        if ($total < 1)
            $data['is_default'] = 1;


        if (!$result = self::$model->add($data))
            return ['code' => 300, 'msg' => '添加失败!'];

        return ['code' => 200, 'msg' => '添加成功', 'data' => ['id' => $result]];
    }

    /**
     * Introduction: 修改数据
     * @author: 王龙
     * @date: 2016/4/18 16:48
     * @email: wanglong@cdlinglu.com
     * @param: $where array 需要修改的地方
     * @param: $data array 修改的数据
     */
    static public function update($data, $action = false)
    {

        if ($action) {

            self::$model->where(['uid' => $data['uid']])->save(['is_default' => 2]);

            if (self::$model->where($data)->save(['is_default' => 1]))
                return ['code' => 200, 'msg' => '设置默认成功!'];

            return ['code' => 300, 'msg' => '设置默认失败!'];

        }

        if (!$data['id'])
            return ['code' => 300, 'msg' => '参数错误,ID不存在!'];

        if (!$data['recipient'])
            return ['code' => 300, 'msg' => '收货人姓名不能为空'];

        if (!$data['mobile'])
            return ['code' => 300, 'msg' => '收货人电话不能为空'];

        if (!$data['sex'])
            return ['code' => 300, 'msg' => '请选择收货人的性别'];

        if (!$data['province'])
            return ['code' => 300, 'msg' => '请选择省份'];

        if (!$data['city'])
            return ['code' => 300, 'msg' => '请选择市'];


        if (!$data['county'])
            return ['code' => 300, 'msg' => '请选择县区'];


        if (!$data['address'])
            return ['code' => 300, 'msg' => '请输入详细地址'];

        $uid = $data['uid'];
        $id = $data['id'];

        unset($data['uid']);
        unset($data['id']);

        if ($data['is_default'] && $data['is_default'] == 1)
            self::$model->where(['uid' => $uid])->save(['is_default' => 2]);

        $result = self::$model->where(['uid' => $uid, 'id' => $id])->save($data);

        if ($result === false)
            return ['code' => 300, 'msg' => '修改失败!'];

        return ['code' => 200, 'msg' => '修改成功!'];

    }

    /**
     * Introduction: 删除数据
     * @author: 王龙
     * @date: 2016/4/18 16:58
     * @email: wanglong@cdlinglu.com
     * @param: $where array 需要修改的地方
     */
    static public function delete($data)
    {


        if (!$data['uid'])
            return ['code' => 500, 'msg' => '请登录!'];

        $result = self::$model->where("uid = " . $data['uid'] . " AND id IN(" . trim($data['ids'], ',') . ")")->delete();

        if (!$result)
            return ['code' => 300, 'msg' => '删除地址失败'];

        return ['code' => 200, 'msg' => '删除地址成功'];
    }

}